package hadoop.topN.question2;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class StuBean implements WritableComparable<StuBean> {

    private String stuName;
    private String courseName;
    private double selfAvg;

    //先以课程名称排序，再以分数降序排序
    @Override
    public int compareTo(StuBean o) {
        if (selfAvg > o.getSelfAvg()) {
            return -1;
        } else if (selfAvg < o.getSelfAvg()) {
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeUTF(this.stuName);
        dataOutput.writeUTF(this.courseName);
        dataOutput.writeDouble(this.selfAvg);
    }

    @Override
    public void readFields(DataInput dataInput) throws IOException {
        this.stuName = dataInput.readUTF();
        this.courseName = dataInput.readUTF();
        this.selfAvg = dataInput.readDouble();
    }

    public StuBean() {
    }

    public StuBean(String stuName, String courseName, double selfAvg) {
        this.stuName = stuName;
        this.courseName = courseName;
        this.selfAvg = selfAvg;
    }

    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public String getCourseName() {
        return courseName;
    }

    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }

    public double getSelfAvg() {
        return selfAvg;
    }

    public void setSelfAvg(double selfAvg) {
        this.selfAvg = selfAvg;
    }

    @Override
    public String toString() {
        return courseName + '\t' + stuName + '\t' + String.format("%.1f", selfAvg);
    }
}
